package com.example.testdemo.coordinator;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.testdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangwenxiang on 2018/8/6.
 */

public class CoordinatorActivity3 extends FragmentActivity {
    private GridView mGridview;
    private AppBarLayout appBarLayout;
    private RecyclerView mRecyleView;
    private SwipeRefreshLayout refreshLayout;
    private List<String> mDatas;
    private Context context;

    private RelativeLayout rl_head_bg;
    private LinearLayout ll_icon_layout;
    private LinearLayout ll_icon_child_layout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acivity_coordintor3_view);
        this.context = this;

        initView();
    }

    private void initView(){
        mDatas = new ArrayList<>();
//        mGridview = findViewById(R.id.gv_data);
//        mGridview.setAdapter(new GridAdapter());
        mRecyleView = findViewById(R.id.rv_data);
        refreshLayout = findViewById(R.id.refresh_layout);
        rl_head_bg = findViewById(R.id.rl_head_bg);
        ll_icon_layout = findViewById(R.id.ll_icon_layout);
        ll_icon_child_layout = findViewById(R.id.ll_icon_child_layout);
        refreshLayout.setRefreshing(false);

        for (int i = 0; i< 100; i++){
            mDatas.add("这是个"+i);
        }

        mRecyleView.setLayoutManager(new WrapContentGridLayoutManager(this, 3));
        mRecyleView.setAdapter(new RecycleAdapter());


        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(false);
            }
        });

        //解决refreshlayout 与appbar冲突
        appBarLayout =  findViewById(R.id.appbar);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //防止刷新冲突
                if (verticalOffset >= 0) {
                    refreshLayout.setEnabled(true);
                } else {
                    refreshLayout.setEnabled(false);
                }
                //渐变
                float alpha =  1 - (float) Math.abs(verticalOffset) / (float)(rl_head_bg.getHeight()  / 3);
//                float alpha = rl_head_bg.getHeight() / Math.abs(verticalOffset);
                ll_icon_layout.setAlpha(alpha);
                //缩放控制
                float scale =  1 - (float) Math.abs(verticalOffset) / (rl_head_bg.getHeight());
                ll_icon_child_layout.setScaleX(scale);
                ll_icon_child_layout.setScaleY(scale);
                //位置
                ll_icon_child_layout.setPivotX(0);
//                ll_icon_child_layout.setPivotY(0);
                Log.e("appbarlayout", "verticalOffset="+verticalOffset+"   height = "+rl_head_bg.getHeight() + " alpha="+alpha);
            }
        });
    }


    public class GridAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int i) {
            return mDatas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view == null){
                holder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.item_prize, null);
                holder.textView = view.findViewById(R.id.prize_text);
                view.setTag(holder);
            }else{
                holder = (ViewHolder) view.getTag();
            }
            holder.textView.setText(mDatas.get(i));
            return view;
        }

        class ViewHolder{
            TextView textView;
        }
    }

    public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder>{

        @Override
        public RecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_prize, parent, false);
            ViewHolder holder = new ViewHolder(itemView);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecycleAdapter.ViewHolder holder, int position) {
            holder.textView.setText(mDatas.get(position));

        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }
        class ViewHolder extends RecyclerView.ViewHolder{
            TextView textView;

            public ViewHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.prize_text);
            }
        }
    }

}
